/*
 * $Id: SelectedPhraseOutAction.java 3 2011-12-26 07:48:43Z forwardly@hotmail.com $
 * 
 * Copyright (c) 2001-2008 Accentrix Company Limited. All Rights Reserved.
 * 
 * Accentrix Company Limited. ("Accentrix") retains copyright on all text, source
 * and binary code contained in this software and documentation. Accentrix grants
 * Licensee a limited license to use this software, provided that this copyright
 * notice and license appear on all copies of the software. The software source
 * code is provided for reference purposes only and may not be copied, modified 
 * or distributed.
 * 
 * THIS SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS," WITHOUT ANY WARRANTY OF
 * ANY KIND UNLESS A SEPARATE WARRANTIES IS PURCHASED FROM ACCENTRIX AND REMAINS
 * VALID.  ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ACCENTRIX SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING THE
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * IN NO EVENT WILL ACCENTRIX BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE
 * USE OF OR INABILITY TO USE SOFTWARE, EVEN IF ACCENTRIX HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */
package com.lightsaber.trade.as.web.category;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;

import com.lightsaber.trade.as.entity.category.CategoryLeaf;
import com.lightsaber.trade.as.entity.category.SelectedPhraseOut;
import com.lightsaber.trade.as.entity.programme.Programme;
import com.lightsaber.trade.as.service.category.CategoryNodeService;
import com.lightsaber.trade.as.service.category.SelectedPhraseOutManager;
import com.lightsaber.trade.as.service.programme.ProgrammeManager;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.web.BaseAction;

@Namespace("/category")
@Action(value = "selectedPhraseOut")
@Result(name = "ADD-PHASE-OUT-SECCUSS", location = "categoryProgrammeAttribute.action", type = "redirect")
public class SelectedPhraseOutAction extends BaseAction {

    private static final long serialVersionUID = -104374405642578033L;

    @Autowired
    private transient ProgrammeManager programmeManager;
    @Autowired
    SelectedPhraseOutManager selectedPhraseOutManager;
    @Autowired
    private transient CategoryNodeService categoryNodeService;

    private List<SelectedPhraseOut> selectedPhraseOuts;
    private String categoryId;
    private String categoryNodeId;
    private String categoryNodeType;

    private String[] ids;
    private List<Programme> phraseOuts;
    private List<String> selectProgrammeList;

    private Page<Programme> page;
    @Value(value = "#{appProperties['programme.page.size']}")
    private String pageSize;

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        selectedPhraseOuts = selectedPhraseOutManager.findByCategoryLeafId(categoryNodeId);
        setCategoryId(categoryId);
        setCategoryNodeId(categoryNodeId);
        setCategoryNodeType(categoryNodeType);
        return "selected-phrase-out";
    }

    public String delete() throws Exception {
        selectedPhraseOutManager.delete(selectProgrammeList);
        setCategoryNodeId(categoryNodeId);
        setCategoryId(categoryId);

        setCategoryNodeType(categoryNodeType);
        addActionMessage(getText(PROCESSED_SUCCESS));

        return list();
    }

    @Override
    public String input() throws Exception {
        List<Programme> programmes = programmeManager
                .filterByAttributes(categoryId, categoryNodeId);
        phraseOuts = programmeManager.filterPhraseOutFromProgrammes(programmes);

        return INPUT;
    }

    public String turnToPhaseOutProgrammePage() throws Exception {
        if (page == null) {
            page = new Page<Programme>(Integer.valueOf(pageSize));
        } else {
            page.setPageSize(Integer.valueOf(pageSize));
        }

        if (!page.isOrderBySetted()) {
            page.setOrderBy(BaseEntity.ID);
            page.setOrder(Page.ASC);
        }
        CategoryLeaf leaf = categoryNodeService.findLeafById(categoryNodeId);
        programmeManager.viewResultSet(categoryId, leaf, page);
        return "preview-phase-out-programme";
    }

    public String save() throws Exception {
        selectedPhraseOutManager.save(selectProgrammeList, categoryNodeId);
        return list();
    }

    public List<SelectedPhraseOut> getSelectedPhraseOuts() {
        return selectedPhraseOuts;
    }

    public void setSelectedPhraseOuts(final List<SelectedPhraseOut> selectedPhraseOuts) {
        this.selectedPhraseOuts = selectedPhraseOuts;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(final String categoryId) {
        this.categoryId = categoryId;
    }

    public List<Programme> getPhraseOuts() {
        return phraseOuts;
    }

    public void setPhraseOuts(final List<Programme> phraseOuts) {
        this.phraseOuts = phraseOuts;
    }

    public String[] getIds() {
        return ids;
    }

    public void setIds(final String[] ids) {
        this.ids = ids;
    }

    public Page<Programme> getPage() {
        return page;
    }

    public void setPage(final Page<Programme> page) {
        this.page = page;
    }

    public String getCategoryNodeId() {
        return categoryNodeId;
    }

    public void setCategoryNodeId(final String categoryNodeId) {
        this.categoryNodeId = categoryNodeId;
    }

    public List<String> getSelectProgrammeList() {
        return selectProgrammeList;
    }

    public void setSelectProgrammeList(final List<String> selectProgrammeList) {
        this.selectProgrammeList = selectProgrammeList;
    }

    public String getPageSize() {
        return pageSize;
    }

    public void setPageSize(String pageSize) {
        this.pageSize = pageSize;
    }

    public String getCategoryNodeType() {
        return categoryNodeType;
    }

    public void setCategoryNodeType(String categoryNodeType) {
        this.categoryNodeType = categoryNodeType;
    }

}
